package com.api.gbhg.zengping.action;

import com.api.gbhg.lbj.tool.Tools;
import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.api.gbhg.luoao.tool.ToolCURD;
import com.api.gbhg.zengping.common.CommonDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @description: 总署下发项目导入
 * @author: Mr.zeng
 * @create: 2021-03-07 10:52
 **/
public class ImportProject implements Action {
    public static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    @Override
    public String execute(RequestInfo requestInfo) {
        LOGGER.info("====================================进入总署下发项目导入ImportProject Action方法=========================================");
        final String requestid = requestInfo.getRequestid();
        String billTableName = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = CommonDao.selectByRequestId(new RecordSet(), billTableName, requestid);


        // 同步流程数据到版本表
        Map<String,String> map=new HashMap<>();
        // 预算总金额通过sql语句修改
        RecordSet rs1 = ToolCURD.selectDetailnanlist(requestInfo, 1);
        BigDecimal total=new BigDecimal("0.0");
        while (rs1.next()){
            total=total.add(new BigDecimal(rs1.getString("sheet4_detail2_field3")));
        }
        while (rs.next()){
        map.put("requestId",requestid);
        map.put("lcid",requestInfo.getWorkflowid());
        map.put("lcbj","18");
        map.put("lcjlsqjlid",requestid);
        map.put("lczt","归档");
        map.put("lcztz","3");
        map.put("sfky","0");
        map.put("xmmin",requestInfo.getDescription());
        map.put("sfzdxm","0");

       /* map.put("cjr",rs.getString("cjr"));
        map.put("cjrq",rs.getString("cjrq"));*/
        map.put("cjrks",rs.getString("cjrks"));
        map.put("cjrdw",rs.getString("cjrdw"));
        map.put("jbr",rs.getString("jbr"));
        map.put("sbdw",rs.getString("bbdw"));
        map.put("sydw",rs.getString("sydw"));
        map.put("xmdm",rs.getString("sheet1_main_field4"));
        map.put("lxdh",rs.getString("sheet1_main_field17"));//联系电话
        map.put("sbdw",rs.getString("sbdw"));//申报单位
        map.put("fzr", Tools.getUseridByName(rs.getString("sheet1_main_field15")));//负责人
        String sbks = DataBaseUtil.getDepartmentIdById(rs.getInt("cjr"));
        map.put("sbks",sbks);//申报科室
        map.put("yszje", String.valueOf(total));
//        map.put("yxysswy",rs.getString("yxysswy"));
        }
        // 同步中央财政拨款项目  dt2
        String mainid = rs.getString("id");
        //得到明细表名
        DetailTable[] detailTables = CommonDao.getDetailTables(requestInfo);
        RecordSet recordSet = CommonDao.selectByDetailTable(detailTables[1].getTableDBName(), mainid);
        Map<String,String>[] maps=new HashMap[recordSet.getArray().size()];
        int i=0;
/*        while (recordSet.next()){
            maps[i] = new HashMap<>();
            maps[i].put("srdw",rs.getString("bbdw"));
            maps[i].put("srnf","2021");
            maps[i].put("gnkm",rs.getString("sheet1_main_field2"));
            maps[i].put("xmmc",rs.getString("sheet1_main_field5"));
            maps[i].put("bnyswy",rs.getString("exyss"));
            maps[i].put("yhpjewy","0.0");
            maps[i].put("djjewy","0.0");
            maps[i].put("ybxje","0.0");
            maps[i].put("syjewy",rs.getString("exyss"));
//            i++;
            String[] fieldName =null;
            if(fieldName==null){
                fieldName=maps[i].keySet().toArray(new String[maps[i].keySet().size()]);
            }

            Object[] values = maps[i].values().toArray();

            String tableName = "uf_zyczbkxm";
            Integer modeid = DataBaseUtil.getModeId(tableName);
            String uuid = UUID.randomUUID().toString();
            String uf_zyczbkxm = DataBaseUtil.getModeInsertSql(Arrays.asList(fieldName), tableName,uuid,modeid);
//            String uf_zyczbkxm = VersionFlowUtil.getInsertSql(Arrays.asList(fieldName), "uf_zyczbkxm");
            RecordSetTrans rst=new RecordSetTrans();
            rst.setAutoCommit(false);
            boolean flag=false;
            LOGGER.info("18.中央财政拨款同步-->"+uf_zyczbkxm);
            try {
                rst.executeUpdate(uf_zyczbkxm,values);
                DataBaseUtil.setPower(tableName,uuid,modeid);
                rst.commit();
                flag=true;
            } catch (Exception e) {
                e.printStackTrace();
                rst.rollback();
            }
            if(!flag){
                LOGGER.info("============================中央财政拨款项目同步失败,数据同步异常======================================");
                requestInfo.getRequestManager().setMessagecontent("中央财政拨款项目同步失败,数据同步异常");
                requestInfo.getRequestManager().setMessageid("1");
                return FAILURE_AND_CONTINUE;
            }
            i++;
        }*/



        String[] fieldName = map.keySet().toArray(new String[map.keySet().size()]);
        Object[] values = map.values().toArray();
        String uf_jingfei_version = VersionFlowUtil.getInsertSql(Arrays.asList(fieldName), "uf_jingfei_version");
        RecordSetTrans rst=new RecordSetTrans();
        rst.setAutoCommit(false);
        boolean flag=false;
        LOGGER.info("18.总署下发项目-->"+uf_jingfei_version);

        map.forEach((key,value)->{
           LOGGER.info(value);
        });

        try {
            rst.executeUpdate(uf_jingfei_version,values);
            rst.commit();
            flag=true;
        } catch (Exception e) {
            e.printStackTrace();
            rst.rollback();
        }
        if(!flag){
            LOGGER.info("============================总署下发项目导入同步失败,数据同步异常======================================");
            requestInfo.getRequestManager().setMessagecontent("总署下发项目导入同步失败");
            requestInfo.getRequestManager().setMessageid("1");
            return FAILURE_AND_CONTINUE;
        }
        return SUCCESS;
    }
}
